<?php
namespace app\admin\controller\system\adminuser;

use app\common\model\AdminRole as Model;

use app\admin\classes\BasicManager;
use core\Api;
use core\V;
use core\Res;
use core\Func;
use core\modules\AcManager;

/**
 * 权限管理
 */


 class AdminRoleManager extends BasicManager {

   static $model=Model::class;
   public $acm;


   static $var="id,name,permission,status,remark";

   static $title=[
     "index"=>[
       "title"=>"后台角色管理",
       "breadcrumb"=>["系统设置","后台角色","列表"],
       "subtitle"=>"管理后台角色",
     ],
     "add"=>[
       "title"=>"添加后台角色",
       "breadcrumb"=>["系统设置","后台角色","添加"],
       "subtitle"=>"添加新的后台角色",
     ],
     "edit"=>[
       "title"=>"编辑后台角色",
       "breadcrumb"=>["系统设置","后台角色","编辑"],
       "subtitle"=>"编辑后台角色信息",
     ],

   ];

   // 前置方法
   protected function initialize(){
     $this->acm=new AcManager();
     parent::initialize();
   }

   // protected function getMenu($menus=""){
   //   $result=[];
   //   if (empty($menus)){
   //     $result["super"]="超级管理员";
   //   }
   //
   //   if (empty($menus)) $menus=config("menu.");
   //   foreach ($menus as $m) {
   //     if (!empty($m["children"])){
   //       $r=$this->getMenu($m["children"]);
   //       $result=array_merge($result,$r);
   //     }
   //     if (!empty($m["type"])){
   //       if ($m["type"]=="menu"){
   //         $ac=empty($m["href"])?"":$m["href"];
   //         if (!empty($ac)){
   //           $result[$ac]=$m["title"];
   //         }
   //
   //       }
   //     }
   //   }
   //   return $result;
   //
   // }
   // protected function getMenuTree($menus=""){
   //   $result=[];
   //   if (empty($menus)){
   //     $result[]=[
   //       "id"=>"super",
   //       "label"=>"超级管理员",
   //     ];
   //   }
   //
   //   if (empty($menus)) $menus=config("menu.");
   //   foreach ($menus as $m) {
   //
   //     if (!empty($m["type"])){
   //
   //       if ($m["type"]=="menu"){
   //         $item=[
   //           "id"=>$m["href"],
   //           "label"=>$m["title"],
   //         ];
   //         $result[]=$item;
   //       }elseif($m["type"]=="group"){
   //         $item=[
   //           "id"=>"_id".uniqid(),
   //           "label"=>$m["title"],
   //         ];
   //         if (!empty($m["children"])){
   //           $item["children"]=$this->getMenuTree($m["children"]);
   //         }
   //         $result[]=$item;
   //       }
   //     }
   //   }
   //   return $result;
   //
   // }
   // protected function getPermissionName($id){
   //   static $_store;
   //   if (empty($_store)){
   //     $_store=$this->getMenu();
   //   }
   //
   //   $ids=Func::json2arr($id);
   //
   //   $result=[];
   //   foreach($ids as $v) {
   //     if (isset($_store[$v])){
   //       $result[]=$_store[$v];
   //     }
   //   }
   //
   //   return Func::arr2str($result," , ");
   // }

   public function getlist_search(&$query){
     $query->removeOption('where');
     parent::getlist_search($query);
   }
   public function getlist_filter(&$item){
     return [
       "permission"=>$this->acm->getNameArr($item->permission,true),
     ];
   }
   public function add_filter(){
     return [
       "act"=>"add",
       "status"=>1,
       "permission"=>[],
     ];
   }
   public function edit_filter(&$item){
     return [
       // "permission"=>Func::json2arr($item["permission"]),
     ];
   }
   public function save_filter(&$data){
     empty($data["name"]) and Api::error('请输入名称');

     $act=input("act");
     if ($act=="add"){
       if (Model::where("name",$data["name"])->count()){
         Api::error('名称已存在');
       }
     }

     return $data;
   }

   public function edit_body(){

     $menuTree=Func::arr2json($this->acm->getTree());
     $this->VD([
       "___vue___"=>'
       data() {
         return {
           permissionList:'.$menuTree.'
         }
       }
       '
     ]);
     return '


     <el-form-item label="名称">
       <el-input v-model="form.name"></el-input>
     </el-form-item>

     <el-form-item label="权限">
       <c-tree v-model="form.permission" :data="permissionList"></c-tree>
     </el-form-item>

     <el-form-item label="是否启用">
       <c-status v-model="form.status"></c-status>
     </el-form-item>

     <el-form-item label="备注">
       <el-input type="textarea" v-model="form.remark"></el-input>
     </el-form-item>
     ';
   }
   public function index_body(){



     $this->VD([
       "___css___"=>'
        .tablebtn-del{
          display:none;
        }
       '
     ]);
     return '
     <el-table-column
       prop="id"
       label="ID"
       width="60">
     </el-table-column>

     <el-table-column
       prop="name"
       label="名称"
       width="120">
     </el-table-column>

     <el-table-column
       prop="permission"
       label="权限">
     </el-table-column>

     <el-table-column
       prop="remark"
       label="备注"
       width="200">
     </el-table-column>

     <el-table-column
       prop="status"
       label="状态"
       width="60">
       <template slot-scope="scope">
        <el-tag
          v-if="scope.row.status==1"
          size="mini"
          type="success">启用</el-tag>
        <el-tag
          v-if="scope.row.status==2"
          size="mini"
          type="danger">禁用</el-tag>
       </template>
     </el-table-column>


     ';
   }
   public function search_body(){
     return '
     <el-form-item label="">
       <el-input
         v-model="form.name"
         @keyup.enter.native="Search"
         placeholder="名称">
       </el-input>
     </el-form-item>
     <el-form-item label="">
       <el-input
         v-model="form.remark"
         @keyup.enter.native="Search"
         placeholder="备注">
       </el-input>
     </el-form-item>
     <el-form-item label="">
       <el-select @change="Search" v-model="form.status" placeholder="状态" style="width:80px;">
         <el-option label="全部" value=""></el-option>
         <el-option label="启用" value="1"></el-option>
         <el-option label="禁用" value="2"></el-option>
       </el-select>
     </el-form-item>
     ';
   }



 }
